package com.mxdx.edu.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.mxdx.edu.util.StringUtils;

/**
 * 拦截器基类
 * 
 * @author wangyuan
 * @date 2016-11-24 13:38
 * 
 */
@Component
public class BaseInterceptor extends HandlerInterceptorAdapter {

    protected boolean sendWriter(HttpServletRequest request, HttpServletResponse response,
            String backUrl) throws IOException {

        String url = String.format("%s%s", request.getContextPath(), backUrl);
        // 异步请求返回一个可跳转的地址
        if (!StringUtils.isEmpty(request.getHeader("X-Requested-With"))) {
            // 将重新登录url存放在Header返给ajax请求者
            response.setHeader("backUrl", url);
        } else {
            response.sendRedirect(url);
        }
        return false;
    }
}
